package com.zh.freechat.assemble;

import com.zh.freechat.common.resp.UserContactsVO;
import com.zh.freechat.domain.user.entity.ChatUser;
import com.zh.freechat.domain.user.entity.UserContacts;
import com.zh.freechat.domain.user.entity.UserRelationFriend;
import com.zh.webcommon.common.support.CopyFieldUtil;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.zh.mycommon.viewdata.ToWrapper.foreach;

/**
 * @author ZH
 * @date 13:50 2021/8/18
 */
public final class UserContactsWrapper {

    public static UserContactsVO convertVO(UserContacts contacts) {
        UserContactsVO vo = new UserContactsVO();

        vo.setUserId(contacts.getUserId());
        vo.setFriends(convertItemWithFriend(contacts.getFriends()));
        vo.setBlackIds(foreach(contacts.getBlacks(), (e) -> e.getBlackId()));
        return vo;
    }

    private static List<UserContactsVO.ContactsItem> convertItemWithFriend(List<UserRelationFriend> friends) {
        return friends.stream()
                .map(e -> {
                    ChatUser chatUser = e.getChatUser();
                    UserContactsVO.ContactsItem item = new UserContactsVO.ContactsItem(e.getId().toString(), chatUser.getUid(), chatUser.getUsername());
                    item.setAliasName(StringUtils.hasText(e.getAliasName()) ? e.getAliasName() : chatUser.getNickname());
                    item.writeFirstLetter();
                    return CopyFieldUtil.copyProperties(chatUser, item);
                })
                .sorted(Comparator.comparing(UserContactsVO.ContactsItem::getAliasName))
                .collect(Collectors.toList());
    }

//    private static List<UserContactsVO.ContactsItem> convertItemWithBlack(List<UserRelationBlack> blacks) {
//        return blacks.stream()
//                .map(e -> {
//                    return new UserContactsVO.ContactsItem(e.getBlackId(), "");
//                 })
//                .collect(Collectors.toList());
//    }
}
